package com.luke.system.service.impl;

import java.util.List;

import com.luke.base.domain.LkUser;
import com.luke.base.mapper.LkUserMapper;
import com.luke.common.utils.StringUtils;
import com.luke.system.domain.SysNotice;
import com.luke.system.domain.SysNoticeUser;
import com.luke.system.mapper.SysNoticeMapper;
import com.luke.system.mapper.SysNoticeUserMapper;
import com.luke.system.service.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 公告 服务层实现

 * 
 * @author luke
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService
{
    @Autowired
    private SysNoticeMapper noticeMapper;
    @Autowired
    private SysNoticeUserMapper noticeUserMapper;
    @Autowired
    private LkUserMapper userMapper;

    /**
     * 查询公告信息
     * 
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId)
    {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     * 
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice)
    {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNotice(SysNotice notice)
    {
        int rows=noticeMapper.insertNotice(notice);

        //保存接收人
        if("1".equals(notice.getReceiver())){
            Long[] userIds = notice.getUserIds();
            if (StringUtils.isNotNull(userIds)){ //如果是指定接收人
                for (Long userId : userIds) {
                    SysNoticeUser noticeUser=new SysNoticeUser();
                    noticeUser.setNoticeId(notice.getNoticeId());
                    noticeUser.setUserId(userId);
                    noticeUserMapper.insertSysNoticeUser(noticeUser);
                }
            }
        }else { //全部接收人
            List<LkUser> userList=userMapper.selectLkUserList(new LkUser());
            for (LkUser user : userList) {
                SysNoticeUser noticeUser=new SysNoticeUser();
                noticeUser.setNoticeId(notice.getNoticeId());
                noticeUser.setUserId(user.getUserId());
                noticeUserMapper.insertSysNoticeUser(noticeUser);
            }
        }
        return rows;
    }

    /**
     * 修改公告
     * 
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice)
    {
        //删除接收人
        noticeUserMapper.deleteSysNoticeUserByNoticeId(notice.getNoticeId());
        //保存接收人
        if("1".equals(notice.getReceiver())){
            Long[] userIds = notice.getUserIds();
            if (StringUtils.isNotNull(userIds)) {
                for (Long userId : userIds) {
                    SysNoticeUser noticeUser = new SysNoticeUser();
                    noticeUser.setNoticeId(notice.getNoticeId());
                    noticeUser.setUserId(userId);
                    noticeUserMapper.insertSysNoticeUser(noticeUser);
                }
            }
        }else { //全部接收人
            List<LkUser> userList=userMapper.selectLkUserList(new LkUser());
            for (LkUser user : userList) {
                SysNoticeUser noticeUser=new SysNoticeUser();
                noticeUser.setNoticeId(notice.getNoticeId());
                noticeUser.setUserId(user.getUserId());
                noticeUserMapper.insertSysNoticeUser(noticeUser);
            }
        }
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     * 
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId)
    {
        noticeUserMapper.deleteSysNoticeUserByNoticeId(noticeId);
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     * 
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds)
    {
        noticeUserMapper.deleteSysNoticeUserByNoticeIds(noticeIds);
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }

    @Override
    public List<SysNotice> selectNoticeListByUserId(Long userId) {
        return noticeMapper.selectNoticeListByUserId(userId);
    }

    @Override
    public int noticeRead(Long noticeId, Long userId) {
        return noticeUserMapper.noticeRead(noticeId,userId);
    }
}
